package com.hidao.platform.zike.config;

import com.hidao.platform.ssh.web.basic.CommonInterceptor;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.Ordered;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Properties;

@SuppressWarnings("deprecation")
@Configuration
@EnableWebMvc
@ImportResource(value={"classpath:config/spring-shiro.xml"})
@PropertySource(value={"classpath:config/config.properties","classpath:config/jdbc.properties"})
public class WebConfig extends WebMvcConfigurerAdapter  {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
		registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
	}
	@Bean
	public EmbeddedServletContainerFactory servletContainer() {
		TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
		return factory;
	}
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.viewResolver(getViewResolver());
	}
	@Bean
	public SimpleMappingExceptionResolver exceptionResolver() {
		SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
		Properties exceptionMappings = new Properties();
		exceptionMappings.put("java.lang.Exception", "common/500");
		exceptionMappings.put("java.lang.RuntimeException", "common/404");
		exceptionResolver.setExceptionMappings(exceptionMappings);
		Properties statusCodes = new Properties();
		statusCodes.put("common/404", "404");
		statusCodes.put("common/500", "500");
		exceptionResolver.setStatusCodes(statusCodes);
		return exceptionResolver;
	}

	 @Bean(name = "multipartResolver")
     public MultipartResolver multipartResolver(){
      CommonsMultipartResolver resolver = new CommonsMultipartResolver();
      resolver.setDefaultEncoding("UTF-8");
      resolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析，以在在UploadAction中捕获文件大小异常
      resolver.setMaxInMemorySize(40960);
      resolver.setMaxUploadSize(50*1024*1024);//上传文件大小 50M 50*1024*1024
      return resolver;
  	}

	@Bean
	public ViewResolver getViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

	@SuppressWarnings("deprecation")
	@Bean
	public FilterRegistrationBean characterEncodingFilter() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		CharacterEncodingFilter filter = new CharacterEncodingFilter();
		filter.setEncoding("UTF-8");
		filterRegistrationBean.setFilter(filter);
		filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
		return filterRegistrationBean;
	}
	@Bean(name ="commonInterceptor")
	public HandlerInterceptor commonInterceptor(){
		return new CommonInterceptor();
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(commonInterceptor()).addPathPatterns("/**");
    }

	@Bean
	public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
		return new RestTemplate(factory);
	}

	@Bean
	public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setReadTimeout(5000);
		factory.setConnectTimeout(15000);
		return factory;
	}
	
	@Bean(name="activityTaskExecutor")
	public TaskExecutor activityTaskExecutor(){
		ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();  
		//线程池所使用的缓冲队列  
		poolTaskExecutor.setQueueCapacity(200);  
		//线程池维护线程的最少数量  
		poolTaskExecutor.setCorePoolSize(5);  
		//线程池维护线程的最大数量  
		poolTaskExecutor.setMaxPoolSize(1000);  
		//线程池维护线程所允许的空闲时间  
		poolTaskExecutor.setKeepAliveSeconds(30000);  
		poolTaskExecutor.initialize();  
		return poolTaskExecutor;
	}
	
}
